Thread: how to cast unsigned char[3] to int?

  1. #1
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332

    how to cast unsigned char[3] to int?

    I have a 3 byte unsigned char array that contains a length value (someone else's control block) and I need to move the value to my int field.

    I tried different flavors of casting, but couldn't come up with a method that didn't ring compiler bells or worked. I ended up using a union, but I am wondering if there is a simpler/better/more proper method.

    Code:
    #include <stdio.h>                                                       
                                                                             
    union MyUnion {                                                          
      char ctemp[4] ;                                                        
      int  itemp ;                                                           
    } ;                                                                      
                                                                             
    int main(void) {                                                         
                                                                             
      union MyUnion myunion ;                                                
      myunion.itemp = 0 ;                                                    
                                                                             
      unsigned char c_len[3] = {0,0,5} ;                                     
                                                                             
      for (int i = 0 ; i < 3 ; i++ ) printf("c_len[%d]=%d\n", i, c_len[i]) ; 
                                                                             
      for (int i = 1 ; i < 4 ; i++ ) myunion.ctemp[i] = c_len[i-1] ;         
                                                                             
      printf("myunion.itemp = %d\n", myunion.itemp) ;                        
                                                                             
      return 0 ;                                                             
    }

    Output:
    Code:
    c_len[0]=0        
    c_len[1]=0        
    c_len[2]=5        
    myunion.itemp = 5
    Mainframe assembler programmer by trade. C coder when I can.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Well decide whether it's little endian or big endian.

    But something like this is usually the way to go.
    Code:
    int result = 0;
    for ( int i = 0 ; i < 3 ; i++ ) {
        result = ( result << 8 ) + c_len[i];
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Clever! My platform is big endian. Will never be ported to another platform. I forgot about shifting. Thank you.
    Mainframe assembler programmer by trade. C coder when I can.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How covert unsigned char[4] to unsigned integer?
    By barracuda in forum C Programming
    Replies: 110
    Last Post: 02-23-2015, 04:00 AM
  2. Best way to cast unsigned int to int?
    By Programmer_P in forum C++ Programming
    Replies: 12
    Last Post: 07-11-2010, 02:34 PM
  3. Replies: 2
    Last Post: 10-06-2009, 09:37 AM
  4. Silent cast to unsigned
    By rasta_freak in forum C Programming
    Replies: 18
    Last Post: 08-06-2008, 06:46 AM
  5. cast unsigned char* to (the default) signed char*
    By Mario F. in forum C++ Programming
    Replies: 24
    Last Post: 07-27-2007, 10:41 AM

Tags for this Thread